{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# aboutSlice\n",
    "\n",
    "\n",
    "> newlist = list[start:end:step] \n",
    "\n",
    "\n",
    "list上的切片操作，如何理解：\n",
    "1. list的每个元素都有一个下标，下标有两种编号方法，从左向右，则从0开始；从右向左，则从-1开始，全是负数\n",
    "2. 切片操作的本质，仍然是基于下标的遍历。\n",
    "3. **步长决定一切**：\n",
    "  1. 步长的正负，决定了切片的方向：正，从左向右；负，从右向左\n",
    "  2. 步长的正负，决定了start 和 end 的值也不一样\n",
    "  3. 更详细地说：正的步长的遍历，是下标变大的过程；负的步长是下标变小的过程。不管采用正下标还是负下标，这个过程都一样。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1]\n",
      "[0, 1]\n",
      "[2, 1]\n",
      "[2, 1]\n",
      "[1]\n"
     ]
    }
   ],
   "source": [
    "# 切片操作的本质，仍然是基于下标的遍历。\n",
    "\n",
    "li = [0,1,2,3,4]\n",
    "def slice(self,start,end,step):\n",
    "    newList = []\n",
    "    if step < 0:\n",
    "        while start>end:\n",
    "            newList.append(self[start])\n",
    "            start = start + step\n",
    "    else:\n",
    "        while start<end:\n",
    "            newList.append(self[start])\n",
    "            start = start + step\n",
    "    return newList\n",
    "print(li[0:2:1])\n",
    "print(slice(li,0,2,1))\n",
    "\n",
    "print(li[2:0:-1])\n",
    "print(slice(li,2,0,-1))\n",
    "print(li[-4:2:1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2 缺省值\n",
    "\n",
    "\n",
    "缺省值或者默认值，仍然先看步长的正负，或者说遍历的方向\n",
    "\n",
    "\n",
    "newList = List[start:end:step]\n",
    "\n",
    "\n",
    "1. 步长为正，缺省的start=0，end = 列表的长度\n",
    "2. 步长为负，缺省的start = -1，end = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4]\n",
      "[4, 3, 2, 1, 0]\n"
     ]
    }
   ],
   "source": [
    "li = [0,1,2,3,4]\n",
    "print(li[::1])\n",
    "print(li[::-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3 正负start和end\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 2]\n",
      "[3, 2, 1, 0]\n"
     ]
    }
   ],
   "source": [
    "li = [0,1,2,3,4]\n",
    "print(li[3:-4:-1])\n",
    "print(li[3::-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4 mod运算\n",
    "\n",
    "\n",
    "如果：\n",
    "$$\n",
    "a\\ mod \\ b = r  \n",
    "$$\n",
    "则必须满足：\n",
    "$$\n",
    "\\begin{align}\n",
    "q | \\frac{a}{b} \\tag{1} \\\\\n",
    "r = a - q \\times b \\tag{2} \\\\\n",
    "r \\in \\{0, \\cdots, b \\} \\tag{3}\n",
    "\\end{align}\n",
    "$$\n",
    "\n",
    "注意：\n",
    "- 模运算和整除运算紧密相关，而且没有表面上看到的那么简单\n",
    "- 关键在于找q，而主要的限制条件是（3）式。\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 1\n",
      "-4 2\n",
      "-4 -2\n",
      "3 -1\n"
     ]
    }
   ],
   "source": [
    "print(10//3,10%3)\n",
    "print(-10//3,-10%3)\n",
    "\n",
    "print(10//-3,10%-3)\n",
    "print(-10//-3,-10%-3)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 10%3 = 1 是如何得到的：\n",
    "  1. 首先找到满足公式（1）的q，这里是3，其它的值得到的r都不满足公式（3）；\n",
    "  2. 其次通过公式（2）计算r，这里是1\n",
    "- 10%3 = 2 是如何得到的：\n",
    "  1. 首先找到满足公式（1）的q，这里是-4，其它的值得到的r都不满足公式（3）；\n",
    "  2. 其次通过公式（2）计算r，这里是2\n",
    "- 10%-3 = -2 是如何得到的：\n",
    "  1. 首先找到满足公式（1）的q，这里是-4，其它的值得到的r都不满足公式（3）；\n",
    "  2. 其次通过公式（2）计算r，这里是-2\n",
    "- 10%-3 = -1 是如何得到的：\n",
    "  1. 首先找到满足公式（1）的q，这里是3，其它的值得到的r都不满足公式（3）；\n",
    "  2. 其次通过公式（2）计算r，这里是-1"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
